import java.awt.*;
import java.awt.event.*;  
import javax.swing.*;

import com.baidu.translate.demo.TransApi;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
//本页面属于GUI编程，用于搭建可视化界面
public class MyFrame implements ActionListener{
	
	private static final String APP_ID = "20211006000965479";
    private static final String SECURITY_KEY = "3oPq1XsnnEgVxrPVYjkm";
    TransApi api = new TransApi(APP_ID, SECURITY_KEY);
    
    Frame frame = new Frame("简易翻译软件");
    TextArea area_up;
    JToolBar toolBar;
    TextArea area_down;
    JComboBox comboBox_down;
    JComboBox comboBox_up;

    public void init() {
    	frame.setSize(1600, 450);
    	frame.setBackground(new Color(255, 255, 255));
    	frame.setLocation(200,400);
    	frame.setResizable(true);//可改变窗口大小
    	
    	toolBar = new JToolBar();//创建工具栏
    	//frame.add(toolBar,BorderLayout.NORTH);
    	JButton btnCopy = new JButton("设置");
    	toolBar.add(btnCopy);
    	Label  label_src=new Label("选择源语言：");
    	
    	Button button_translate = new Button("翻译");
    	button_translate.addActionListener(this);
    	
    	
    	String [] language_src = {"自动", "汉语", "英语"};
    	comboBox_up=new JComboBox(language_src);
    	
    	area_up = new TextArea();
    	area_up.setFont(new Font("黑体",Font.BOLD,20));
    	area_up.setEditable(true);
    	area_up.setColumns(50);
    	area_up.setRows(14);
    	
    	Label  label_dst=new Label("选择要翻译的语言：");
    	
    	String [] language_dst = {"汉语", "英语","日语","韩语","德语","文言文","繁体中文","法语","捷克语"};
    	comboBox_down=new JComboBox(language_dst);
    	
    	area_down = new TextArea();
    	area_down.setFont(new Font("黑体",Font.BOLD,20));
    	area_down.setEditable(true);
    	area_down.setColumns(50);
    	area_down.setRows(14);
    	
    	frame.setLayout(new FlowLayout(FlowLayout.LEFT));
    	frame.add(label_src);
    	frame.add(comboBox_up);
    	frame.add(area_up);
    	frame.add(label_dst);
    	frame.add(comboBox_down);
    	frame.add(area_down);
    	frame.add(button_translate);
    	
    	
    	//监听关闭按钮
    	frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    	
    	frame.setVisible(true);
    	frame.setResizable(false);//窗口大小不可调
    }
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		//System.out.println(area_up.getText());
		String query=area_up.getText();
		if(query.equals("#pre")) {
			System.out.println("hello");
			area_down.setText("pre－表示“….前的，预先”\r\n" + 
					"preschool 学龄前的（pre+school 学校→上学前的） \r\n" + 
					"prehistory 史前(pre+history 历史) \r\n" + 
					"prefix 前缀(pre+fix 固定→固定在前面→前缀) \r\n" + 
					"preposition 前置词,介词(pre+position 位置→位置放在前面\r\n" + 
					"→前置词) \r\n" + 
					"prepay 预付(pre+pay 付钱) \r\n" + 
					"prebuilt 预制的(pre+built 建造) \r\n" + 
					"preamble 前言,绪言(pre+amble 跑→跑在前面→前言) \r\n" + 
					"precaution 预防措施(pre+caution 小心→预先小心→预防) \r\n" + 
					"precede (在时间上)先于,早于(pre+cede 走→走在前面→[时\r\n" + 
					"间上]先于) \r\n" + 
					"precedent 先例(pre+ced 走+ent→走在前面的东西→先例) \r\n" + 
					"precept 规范;格言(pre+cept 拿→以前拿好的[名言]→格言) \r\n" + 
					"precinct 区域,范围(pre+cinct 捆→预先捆好的→范围) \r\n" + 
					"precipice 峭壁(pre+cipice 头→在前面是尽头→峭壁) \r\n" + 
					"precipitation 降落,仓促(pre+cipit 头→ation→落在前头→\r\n" + 
					"降落,下降) \r\n" + 
					"precise 精确的,精细的(pre+cise 切→预先切好的→精确的) \r\n" + 
					"preclude 妨碍;阻止(pre+clude 关闭→提前关闭→阻止) \r\n" + 
					"precocious 早熟的(pre+cocious 熟→提前煮熟→早熟的) \r\n" + 
					"predict 预言,预报(pre+dict 说→预先说→预言) \r\n" + 
					"predisposition 倾向,癖性(pre+disposition 性情→性情预定\r\n" + 
					"→癖性) \r\n" + 
					"preoccupation 全神贯注(pre+occup 占有+ation→优先占有\r\n" + 
					"(注意力) →全神贯注) \r\n" + 
					"prescient 有先见之明的(pre+scient 知道的→预先知道) \r\n" + 
					"prerogative 权利,特权(pre+rog 要求+ative→预先要求→(有)\r\n" + 
					"特权)");
			return ;
		}
		String in_language="",out_language="";
		
		if(comboBox_up.getSelectedIndex()==0) {
			in_language="auto";
		}else if(comboBox_up.getSelectedIndex()==1){
			in_language="zh";
		}else if(comboBox_up.getSelectedIndex()==2){
			in_language="en";
		}
		
		if(comboBox_down.getSelectedIndex()==0) {
			out_language="zh";
		}else if(comboBox_down.getSelectedIndex()==1){
			out_language="en";
		}else if(comboBox_down.getSelectedIndex()==2) {
			out_language="jp";
		}else if(comboBox_down.getSelectedIndex()==3) {
			out_language="kor";
		}else if(comboBox_down.getSelectedIndex()==4) {
			out_language="de";
		}else if(comboBox_down.getSelectedIndex()==5) {
			out_language="wyw";
		}else if(comboBox_down.getSelectedIndex()==6) {
			out_language="cht";
		}else if(comboBox_down.getSelectedIndex()==7) {
			out_language="fra";
		}else if(comboBox_down.getSelectedIndex()==8) {
			out_language="cs";
		}
		String out=api.getTransResult(query, in_language, out_language);
		String out_to_area="";
		JSONObject jsonObject = JSONObject.fromObject(out);
        JSONArray arr = jsonObject.getJSONArray("trans_result");
        System.out.println(out);
        //System.out.println(arr);
        for(int i=0;i<arr.size();i++) {
        	JSONObject subObj = arr.getJSONObject(i);
        	out_to_area=out_to_area+subObj.getString("dst")+"\n";
        }
        area_down.setText("");
		area_down.append(out_to_area);
	}
    
}
